package com.anxin.act.task.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.anxin.act.task.dao.WfActTaskVariableDao;
import com.anxin.act.task.entity.WfActTaskVariable;
import com.anxin.framework.web.service.BaseService;
import com.anxin.common.utils.SecurityUtils;
import com.anxin.sys.system.entity.SysUser;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 任务变量Service业务层处理
 *
 * @author jbs
 * @date 2022-05-19
 */
@Service
public class WfActTaskVariableService extends BaseService<WfActTaskVariableDao, WfActTaskVariable>{
    @Autowired
    private WfActTaskVariableDao wfActTaskVariableDao;

    /**
     * 查询任务变量
     *
     * @param id 任务变量ID
     * @return 任务变量
     */
    public WfActTaskVariable getById(String id){
        return wfActTaskVariableDao.getById(id);
    }

    /**
     * 保存数据
     * @param actId 流程实例ID
     * @param taskId 任务ID
     * @param key 任务Key
     * @param value 任务Value
     */
    public void save(String actId,String taskId,String key,String value){
        WfActTaskVariable wfActTaskVariable = wfActTaskVariableDao.getValue(taskId,key);
        if (wfActTaskVariable == null){
            wfActTaskVariable = new WfActTaskVariable();
        }
        wfActTaskVariable.setActId(actId);
        wfActTaskVariable.setTaskId(taskId);
        wfActTaskVariable.setVariableKey(key);
        wfActTaskVariable.setVariableValue(value);
        super.save(wfActTaskVariable);
    }

    /**
     * 保存流程变量
     * @param actId 流程实例ID
     * @param taskId 任务ID
     * @param var 流程变量
     */
    public void save(String actId, String taskId, Map<String,String> var){
        Set<String> keySet = var.keySet();
        for (String key : keySet) {
            this.save(actId,taskId,key,var.get(key));
        }
    }

    /**
     * 获取任务变量值
     * @param taskId 任务ID
     * @param key 任务Key
     * @return
     */
    public String getValue(String taskId,String key){
        WfActTaskVariable wfActTaskVariable = wfActTaskVariableDao.getValue(taskId, key);
        return wfActTaskVariable != null ? wfActTaskVariable.getVariableValue() : null;
    }


    /**
     * 查询任务变量列表
     *
     * @param wfActTaskVariable 任务变量
     * @return 任务变量
     */
    public List<WfActTaskVariable> findList(WfActTaskVariable wfActTaskVariable){
        return wfActTaskVariableDao.findList(wfActTaskVariable);
    }



    /**
     * 保存任务变量
     *
     * @param wfActTaskVariable 任务变量
     * @return 结果
     */
    public int save(WfActTaskVariable wfActTaskVariable){
        return super.save(wfActTaskVariable);
    }


    /**
     * 删除任务变量信息
     *
     * @param id 任务变量ID
     * @return 结果
     */
    public int deleteById(String id){
        SysUser user = SecurityUtils.getUser();
        return wfActTaskVariableDao.deleteById(id,user.getId());
    }
}
